package com.xiaoyy.core.dict.gateway.controller;

import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageInfo;
import com.xiaoyy.core.common.validators.Validators;
import com.xiaoyy.core.common.vo.*;
import com.xiaoyy.core.dict.gateway.req.*;
import com.xiaoyy.core.dict.gateway.validators.DictValidators;
import com.xiaoyy.core.dict.integration.DictIntegration;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 数据字典管理
 *
 * @author chenzh
 * @date 2019-112-10
 */
@RestController
@RequestMapping(value = "/dict")
public class DictContorller {
    @Autowired
    DictIntegration dictIntegration;
    /**
     * 分页查询数据字典信息
     */
    @PostMapping(value = "/query/list", produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "数据字典列表", notes = "分页查询数据字典信息")
    public AppResponse<PageInfo<DictVO>> getPageList(@RequestBody DictReq dictReq) {
        DictVO dictVO = Convert.convert(DictVO.class,dictReq);
        return AppResponse.successData(dictIntegration.getDictList(dictVO));
    }
    /**
     * 查询单个详情
     */
    @ApiOperation(value = "数据字典详情", notes = "根据id查询详情")
    @PostMapping(value = "/query/detail", produces = "application/json;charset=UTF-8")
    public AppResponse<DictVO> getDetail(@RequestBody @Validated(value = {Validators.GetDetail.class}) DictDetailReq dictReq) {
        DictVO dictVO = Convert.convert(DictVO.class,dictReq);
        return AppResponse.successData(dictIntegration.getDictDetail(dictVO));
    }
    /**
     * 获取字典字段下拉
     */
    @ApiOperation(value = "数据字典字段下拉", notes = "获取所有字典字段下拉")
    @PostMapping(value = "common/query/dictField", produces = "application/json;charset=UTF-8")
    public AppResponse<List<SelectVO>> getDictField(@RequestBody BaseVO baseVO) {
        return AppResponse.successData(dictIntegration.getDictField());
    }
    /**
     * 新增
     */
    @ApiOperation(value = "新增字典", notes = "新增字典信息")
    @PostMapping(value = "/add", produces = "application/json;charset=UTF-8")
    public AppResponse<DictVO> add(@RequestBody @Validated(value = {Validators.Add.class}) DictReq dictReq) {
        DictVO dictVO = Convert.convert(DictVO.class,dictReq);
        return AppResponse.successData(dictIntegration.add(dictVO));
    }
    /**
     * 修改
     */
    @ApiOperation(value = "修改字典", notes = "修改字典信息")
    @PostMapping(value = "/edit", produces = "application/json;charset=UTF-8")
    public AppResponse<DictVO> edit(@RequestBody @Validated(value = {Validators.Edit.class}) DictReq dictReq) {
        DictVO dictVO = Convert.convert(DictVO.class,dictReq);
        return AppResponse.successData(dictIntegration.edit(dictVO));
    }
    /**
     * 删除
     */
    @ApiOperation(value = "单个删除字典", notes = "单个删除字典信息")
    @PostMapping(value = "/delete", produces = "application/json;charset=UTF-8")
    public AppResponse delete(@RequestBody @Validated(value = {Validators.Del.class}) DictDeleteReq dictReq) {
        DictDeleteVO dictVO = Convert.convert(DictDeleteVO.class,dictReq);
        dictIntegration.delete(dictVO);
        return AppResponse.success();
    }
    /**
     * 删除 批量
     */
    @ApiOperation(value = "批量删除字典", notes = "批量删除字典信息")
    @PostMapping(value = "/delete/batch", produces = "application/json;charset=UTF-8")
    public AppResponse deleteBatch(@RequestBody @Validated(value = {Validators.BatchDel.class}) DictDeleteReq dictReq) {
        DictDeleteVO dictVO = Convert.convert(DictDeleteVO.class,dictReq);
        dictIntegration.deleteBatch(dictVO);
        return AppResponse.success();
    }
    /**
     * 清除缓存
     */
    @ApiOperation(value = "清除数据字典缓存", notes = "清除数据字典缓存，不传入参数则清除所有")
    @PostMapping(value = "/clear/cache", produces = "application/json;charset=UTF-8")
    public AppResponse clearCache(@RequestBody DictFieldReq dictReq) {
        dictIntegration.clearCache(dictReq.getDictField());
        return AppResponse.success();
    }
    /**
     * select 初始化 批量
     */
    @ApiOperation(value = "批量获取字典下拉信息", notes = "批量获取字典下拉信息")
    @PostMapping(value = "/common/batch", produces = "application/json;charset=UTF-8")
    public AppResponse<Map<String,List<SelectVO>>> getDictBatch(@RequestBody @Validated(value = {DictValidators.GetDictBatch.class}) SelectReq selectReq) {
        return AppResponse.successData(dictIntegration.getDictBatch(selectReq.getDictFields()));
    }
    /**
     * select 初始化单个
     */
    @ApiOperation(value = "单个获取字典下拉信息", notes = "单个获取字典下拉信息")
    @PostMapping(value = "/common/one", produces = "application/json;charset=UTF-8")
    public AppResponse<List<SelectVO>> getDict(@RequestBody  @Validated(value = {DictValidators.GetDict.class}) SelectReq selectReq) {
        return AppResponse.successData(dictIntegration.getDict(selectReq.getDictField()));
    }

    /**
     * 通用数据字典下拉树
     */
    @ApiOperation(value = "通用获取字典下拉树", notes = "通用获取字典下拉树")
    @PostMapping(value = "/common/tree", produces = "application/json;charset=UTF-8")
    public AppResponse<List<AntTreeVO>> getDictTree(@RequestBody  @Validated(value = {DictValidators.GetDict.class}) SelectReq selectReq) {
        return AppResponse.successData(dictIntegration.getDictCommonTree(selectReq.getDictField()));
    }
    /**
     * 全国行政区划下拉树
     */
    @ApiOperation(value = "获取全国行政区划下拉树", notes = "获取全国行政区划下拉树")
    @PostMapping(value = "/common/areaSelectTree", produces = "application/json;charset=UTF-8")
    public AppResponse<List<AntTreeVO>> getAreaSelectTree(@RequestBody AreaSelectReq areaSelectReq) {
        return AppResponse.successData(dictIntegration.getAreaSelectTree(areaSelectReq.getInData(), areaSelectReq.getNotINData()));
    }

    /**
     * 下拉图标树
     */
    @ApiOperation(value = "获取下拉图标树", notes = "获取下拉图标树")
    @RequestMapping(value = "/common/iconSelectTree", produces = "application/json;charset=UTF-8")
    public AppResponse<List<AntTreeVO>> getIconSelectTree(@RequestBody BaseVO baseVO) {
        return AppResponse.successData(dictIntegration.getIconSelectTree());
    }

}
